package com.ebt.m.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.proposal_v2.bean.event.DraftUpdateCompleteEvent;
import com.ebt.m.proposal_v2.utils.Utils;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.buscomponent.listview.SectionItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by andy on 17-5-31.
 */

public class ProposalListPresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    private int currentPage = 1;
    private int position;

    public ProposalListPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        currentPage = 1;
        position = (int) args[0];
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.fetchProposalList(currentPage, ConfigData.PAGE_COUNT_LIMIT, calculateStatus(position))
                .filter(getFilterForFootviewStatus(true))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    Utils.print("draft", ">>>草稿更新完成");
                    iView.updateData(createSectionItem(response.getData(), position));
                    EventBus.getDefault().post(new DraftUpdateCompleteEvent()); // 通知草稿页面关闭
                }, throwable -> {
                    throwable.printStackTrace();
                    Utils.print("draft", ">>>草稿更新完成");
                    iView.errorData();
                    EventBus.getDefault().post(new DraftUpdateCompleteEvent()); // 通知草稿页面关闭
                });
    }

    @Override
    public void refresh() {
        loadNew(position);
    }

    @Override
    public void loadNext(Object... args) {
        position = (int) args[0];
        EBTAPI erpApi = AppInitService.getEbtApi();
        erpApi.fetchProposalList(currentPage + 1, ConfigData.PAGE_COUNT_LIMIT, calculateStatus(position))
                .filter(getFilterForFootviewStatus(false))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    currentPage++;
                    iView.updateDataAdd(createSectionItem(response.getData(), position));
                }, throwable -> {
                    throwable.printStackTrace();
                    iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                });
    }

    public List<SectionItem> createSectionItem(List list, int position) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null || list.size() < 1) {
            return sections;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                ProposalListItem proposalItem = (ProposalListItem) list.get(i);
                proposalItem.tabPage = position;
                SectionItem item = new SectionItem(position, proposalItem);
                sections.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }

    private int calculateStatus(int position) {
        if (position == 0) {
            return -1;
        }
        return position;
    }

    public void deleteProposal(String pickNum) {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.deleteProposal(pickNum)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(responseBodyResponse -> {
                    try {
                        Logger.e(responseBodyResponse.body().string());
                        ToastUtil.show(mContext, "删除计划书成功");
                        iView.update(position);
                    } catch (Exception e) {
                        Logger.e(e);
                    }
                }, throwable -> {
                    NetUtils.checkHttpException(mContext, throwable);
                    Logger.e(throwable.getMessage());
                });
    }
}
